package com.yylending.las.personal;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yylending.las.Global;
import com.yylending.las.R;
import com.yylending.las.YJKD;
import com.yylending.las.base.BaseFragment;
import com.yylending.las.Constants;
import com.yylending.las.StatIDSet;
import com.yyfax.infrastructure.statistics.StatService;
import com.yylending.las.common.utils.preferenceutils.PreferenceConstants;
import com.yylending.las.main.MainActivity;
import com.yylending.las.personal.bandcard.BankCardActivity;
import com.yylending.las.personal.bandcard.BankCardModel;
import com.yylending.las.personal.bandcard.BankCardPresenter;
import com.yylending.las.personal.bandcard.BoundBankCards;
import com.yylending.las.personal.bandcard.verified.RealNameVerifiedModel;
import com.yylending.las.personal.bandcard.verified.RealNameVerifiedPresenter;
import com.yylending.las.personal.loan.LoanListActivity;
import com.yylending.las.webview.WebViewActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 我页
 * Created by laixj on 2017/5/22.
 * Copyright ©2016 www.yylending.com. All Rights Reserved.
 */
public class PersonalCenterFragment extends BaseFragment implements BankCardPresenter.View, RealNameVerifiedPresenter.View {

    private final static String TAG = "PersonalCenterFragment";

    @BindView(R.id.personal_my_loan_tv)
    TextView mPersonalMyLoanTv;
    @BindView(R.id.personal_feedback_tv)
    TextView mPersonalFeedbackTv;
    @BindView(R.id.personal_about_us_tv)
    TextView mPersonalAboutUsTv;
    @BindView(R.id.personal_security_setting_tv)
    TextView mPersonalSecuritySettingTv;
    Unbinder unbinder;
    @BindView(R.id.personal_center_account_tv)
    TextView mAccountTv;
    @BindView(R.id.has_bind_flag_tv)
    TextView mHasBindFlagTv;


    private BankCardPresenter mBankCardPresenter;
    private RealNameVerifiedPresenter mRealNameVerifiedPresenter;
    private ArrayList<BoundBankCards.BankInfo> mBankInfoList = new ArrayList<>();

    @Override
    public Loader<Presenter> onCreateLoader(int id, Bundle args) {
        switch (id){
            case MainActivity.LOADER_ID_GET_REAL_NAME:
                return new PresenterLoader<>(getActivity(), new RealNameVerifiedPresenter.Generator());
            case MainActivity.LOADER_ID_BANK_CARD:
                return new PresenterLoader<>(getActivity(), new BankCardPresenter.Generator());
            default:
                break;
        }
        return null;
    }

    @Override
    public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
        switch (loader.getId()){
            case MainActivity.LOADER_ID_GET_REAL_NAME:
                mRealNameVerifiedPresenter = (RealNameVerifiedPresenter) data;
                mRealNameVerifiedPresenter.attach(this);
                break;
            case MainActivity.LOADER_ID_BANK_CARD:
                mBankCardPresenter = (BankCardPresenter) data;
                mBankCardPresenter.attach(this);
                break;
            default:
                break;
        }
    }

    public static BaseFragment newInstance(int sectionNumber) {
        BaseFragment fragment = new PersonalCenterFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		CLog.e(TAG, "MoreFragment onViewCreated--");
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        setUserName();
        mBankCardPresenter.getMyBankCard(false);
        mRealNameVerifiedPresenter.getRealNameVerify();
    }

    private void setUserName(){
        if (TextUtils.isEmpty(Global.USER_NAME) || TextUtils.equals(Constants.NO_REAL_VERIFY, Global.USER_NAME)) {
            mAccountTv.setText(Constants.NO_REAL_VERIFY);
        } else {
            mAccountTv.setText(Global.USER_NAME);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_personal_center, container, false);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();

    }

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		getLoaderManager().initLoader(MainActivity.LOADER_ID_GET_REAL_NAME, null, this);
		getLoaderManager().initLoader(MainActivity.LOADER_ID_BANK_CARD, null, this);
	}

	@Override
	public void onDetach() {
		getLoaderManager().destroyLoader(MainActivity.LOADER_ID_GET_REAL_NAME);
		getLoaderManager().destroyLoader(MainActivity.LOADER_ID_BANK_CARD);
		super.onDetach();
	}

	@Override
    public void bindCardSuccess() {

    }

    @Override
    public void bindCardFailed(String msg) {

    }

    @Override
    public void hasBindBankCard(boolean isManual, List<BoundBankCards.BankInfo> bankInfo) {
        mHasBindFlagTv.setVisibility(View.VISIBLE);
        // 显示是否已绑卡的标识.
        mHasBindFlagTv.setText(R.string.has_bind_card);
        if (isManual) {
            mBankInfoList.clear();
            mBankInfoList.addAll(bankInfo);
            BankCardActivity.start(getActivity(), mBankInfoList, null, Constants.KEY_GOTO_BIND_CARD_FROM_PERSONAL_CENTER_FRAGMENT);
        }
    }

    @Override
    public void neverBindBankCard(boolean isManual) {
        mHasBindFlagTv.setVisibility(View.GONE);
        if (isManual) {
            mBankInfoList.clear();
            BankCardActivity.start(getActivity(), mBankInfoList, null, Constants.KEY_GOTO_BIND_CARD_FROM_PERSONAL_CENTER_FRAGMENT);
        }
    }

    @Override
    public void requestBindBankCardFailed(boolean isManual) {
        if (isManual){
            showToast(R.string.get_bind_card_failed);
        }
    }

    @Override
    public void getSupportBankSuccess(List<BankCardModel.SupportBankList.SupportBank> supportBankList) {

    }

    @Override
    public void getSupportBankFailed(String msg) {

    }

	@Override
	public void bindCardRetCode(String retCode) {

	}

    @Override
    public void hasRealNameVerified(RealNameVerifiedModel.RealNameInfo realNameInfo) {
        if (!TextUtils.isEmpty(realNameInfo.iname)){
            if (realNameInfo.iname.contains("*")) {
                Global.USER_NAME = realNameInfo.iname;
            }else {
                Global.USER_NAME = realNameInfo.iname.substring(0, 1) + "**";
            }
            YJKD.getInstance().getPreferenceManager().savePreference(PreferenceConstants.SP_KEY_USER_NAME, Global.USER_NAME);
        }
        setUserName();
    }

    @Override
    public void noRealNameVerified(String msg) {
        Global.USER_NAME = Constants.NO_REAL_VERIFY;
        YJKD.getInstance().getPreferenceManager().savePreference(PreferenceConstants.SP_KEY_USER_NAME, Global.USER_NAME);
        setUserName();
    }

    @Override
    public void noMatchRealName(String msg) {
        Global.USER_NAME = Constants.NO_REAL_VERIFY;
        YJKD.getInstance().getPreferenceManager().savePreference(PreferenceConstants.SP_KEY_USER_NAME, Global.USER_NAME);
        setUserName();
    }

    @Override
    public void getRealNameVerifyFailed(String msg) {
        showToast(msg);
    }

    @OnClick({R.id.personal_my_loan_tv, R.id.personal_bank_card_rl, R.id.personal_feedback_tv, R.id.personal_about_us_tv, R.id.personal_security_setting_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.personal_my_loan_tv:
				StatService.onEvent(getContext(), StatIDSet.EVENT_ID_BORROWING_ENTRANCE);
				LoanListActivity.start(getActivity());
                break;
            case R.id.personal_bank_card_rl:
                mBankCardPresenter.getMyBankCard(true);
                break;
            case R.id.personal_feedback_tv:
				if(!TextUtils.isEmpty(Global.HELP_URL)) {
					WebViewActivity.start(getActivity(), Global.BASE_URL + Global.HELP_URL, getString(R.string.help_center), getString(R.string.title_activity_feedback));
				}
                break;
            case R.id.personal_about_us_tv:
                AboutUsActivity.start(getActivity());
                break;
            case R.id.personal_security_setting_tv:
                SecurityActivity.start(getActivity());
                break;
        }
    }
}
